package com.sgb.contract.view.ui.details;

import android.os.CountDownTimer;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.contract.api.AbstractCallBack;
import com.sgb.contract.api.ContractNet;
import com.sgb.contract.base.viewmodel.BaseViewModel;
import com.sgb.contract.model.ContractApprovalStatusBean;
import com.sgb.contract.model.ContractDetailBean;
import com.sgb.contract.test.model.OpenAccountDetailEntity;
import com.sgb.contract.utils.ArouterConfigs;
import com.sgb.contract.utils.Constants;
import com.sgb.contract.utils.SimpleNotifyListener;
import com.sgb.contract.utils.StringUtils;
import com.sgb.contract.view.widget.CustomDragView;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.lifecycle.MutableLiveData;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import retrofit2.Call;

/**
 * 合同签署
 *
 * @author cyj
 */
public class SignContractViewModel extends BaseViewModel implements SimpleNotifyListener<Object> {

    /**
     * 当前item position位置
     */
    private int pageNum = 1;
    public ObservableField<String> currentPosition = new ObservableField<>(pageNum + "/" + pageNum);
    public ContractDetailBean bean;
    public SignContractAdapter adapter;
    public final int MAX_NUM = 3;
    public ObservableInt maxChapterNum = new ObservableInt(MAX_NUM);
    public ObservableField<String> successDesc = new ObservableField<>("");
    /**
     * 显示签署layout布局，包含取消，添加印章，立即签署功能
     */
    public ObservableBoolean showAddSignLayout = new ObservableBoolean(false);
    /**
     * 显示请前往PC端查看该审批布局
     */
    public ObservableBoolean showInApprovalLayout = new ObservableBoolean(false);
    /**
     * 显示底部操作布局
     */
    public ObservableBoolean showOperationLayout = new ObservableBoolean(true);
    private boolean isSendContract = false;
    // 判断是否显示修改合同按钮
    public ObservableBoolean isShow = new ObservableBoolean(true);
    private boolean isSendApproval = false;
    /**
     * 显示签署成功UI布局
     */
    public ObservableBoolean showSignSuccessLayout = new ObservableBoolean(false);
    /**
     * 显示安心签获取验证码布局
     */
    public ObservableBoolean showValidationCodeLayout = new ObservableBoolean(false);
    /**
     * 显示安心签获取验证码 倒计时 UI
     */
    public ObservableField<String> showValCodeCountdownLayout = new ObservableField<>("60");
    /**
     * 安心签获取验证码
     */
    public ObservableField<String> validationCode = new ObservableField<>("");
    /**
     * 是否显示修改合同的弹窗布局
     */
    public MutableLiveData<Boolean> showRejectContractPop = new MutableLiveData<>();
    /**
     * 修改理由内容
     */
    public ObservableField<String> rejectContractContent = new ObservableField<>("");
    /**
     * 操作按钮修改合同/退回合同 选择名称
     */
    public ObservableField<String> contractOperationName = new ObservableField<>("");
    /**
     * 操作按钮发送合同/签署合同 选择名称
     */
    public ObservableField<String> contractOperationName1 = new ObservableField<>("");
    /**
     * 操作按钮发送合同/签署合同 选择名称
     */
    public ObservableField<String> contractOperationName2 = new ObservableField<>("");
    /**
     * 手机号 内容
     */
    public ObservableField<String> phoneNum = new ObservableField<>("");
    /**
     * 联系人姓名
     */
    public ObservableField<String> name = new ObservableField<>("");
    /**
     * localUrl 图片加载地址为空时，(包含外部请求)，显示无数据
     */
    public MutableLiveData<Object> showNoDataView = new MutableLiveData<>();
    private int mSignStatus;


    public SignContractViewModel() {
        adapter = new SignContractAdapter(application.getApplicationContext(), null);
        //设置印章视图删除更新UI的监听器
        adapter.setViewRemoveListener((obj) -> {
            int maxNum = maxChapterNum.get() + 1;
            maxChapterNum.set(Math.min(maxNum, MAX_NUM));
        });
    }

    /**
     * 退回合同/修改合同
     */
    public void operationContractOnClick() {
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        if (bean.isTaker) {
                            // 发起方  显示作废
                            showDialog("discardContract", "退回合同后不可恢复\n还要继续吗？", "取消", "退回");
                        } else {
                            // 参与方  修改合同 需判断当前合同的审批状态（只有签署合同才走审批）
                            findAuditStatus(bean.id, "signContract", 1);
                        }
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        if (Constants.NET_BSS_ERROR_CODE_539114.equals(entity.getErrCode())) {
                            showDialog("checkApprovalOpenAudit", entity.getMsg());
                        } else {
                            showDialog("checkStatusByNowStatus", entity.getMsg());
                        }
                    }
                });
    }

    /**
     * 修改合同
     */
    public void alterContractOnClick() {
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        // 参与方  修改合同 需判断当前合同的审批状态（只有签署合同才走审批）
                        findAuditStatus(bean.id, "signContract", 4);
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        if (Constants.NET_BSS_ERROR_CODE_539114.equals(entity.getErrCode())) {
                            showDialog("checkApprovalOpenAudit", entity.getMsg());
                        } else {
                            showDialog("checkStatusByNowStatus", entity.getMsg());
                        }
                    }
                });
    }

    /**
     * 退回合同
     */
    public void discardContract() {
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("contractId", bean.id);
                        map.put("localUrl", bean.localUrl);
                        map.put("contractNo", bean.no);
                        map.put("currentStatus", String.valueOf(bean.status));
                        showLoading();
                        ContractNet.getInstance().getContractApi().discardContract(map).enqueue(new AbstractCallBack<BaseEntity<Object>>() {
                            @Override
                            public void onSuccess(BaseEntity<Object> data) {
                                lookContractDetails();
                            }
                        });
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        showDialog("checkStatusByNowStatus", entity.getMsg());
                    }
                });
    }

    /**
     * 修改合同-确认 点击事件
     */
    public void rejectContractSure(String content) {
        showLoading();
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("companyNo", bean.companyNo);
                        map.put("contractNo", bean.no);
                        map.put("currentStatus", bean.status);
                        map.put("id", bean.id);
                        map.put("remark", null == content ? "" : content);
                        ContractNet.getInstance().getContractApi().rejectContract(map).enqueue(new AbstractCallBack<BaseEntity<Object>>() {
                            @Override
                            public void onSuccess(BaseEntity<Object> data) {
                                // 返回列表界面
                                ARouter.getInstance().build(ArouterConfigs.AR_CONTRACT_LIST).withString("contractId", String.valueOf(bean.id)).navigation();
                                // finnish
                                finishActivity();
                            }
                        });
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        showDialog("checkStatusByNowStatus", entity.getMsg());
                    }
                });

    }

    /**
     * 发送合同/签署合同 点击事件
     */
    public void contractSignOnClick() {
        showLoading();
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        if (isSendContract) {
                            // 发送合同
                            sendContract();
                        } else if (isSendApproval) {
                            //提交审批 (签署合同才有审批)
                            checkApprovalOpenAudit(bean.id, "signContract", 2);
                        } else {
                            //签署合同，判断是否开启审批
                            checkApprovalOpenAudit(bean.id, "signContract", 3);
                        }
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        if (Constants.NET_BSS_ERROR_CODE_539114.equals(entity.getErrCode())) {
                            showDialog("checkApprovalOpenAudit", entity.getMsg());
                        } else {
                            showDialog("checkStatusByNowStatus", entity.getMsg());
                        }
                    }
                });
    }

    /**
     * 发送合同
     */
    private void sendContract() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", bean.id);
        map.put("contractNo", bean.no);
        map.put("currentStatus", String.valueOf(bean.status));
        showLoading();
        ContractNet.getInstance().getContractApi().sendContract(map).enqueue(new AbstractCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                lookContractDetails();
            }
        });
    }

    /**
     * 签署合同-取消 点击事件
     */
    public void contractSignCancelOnClick() {
        maxChapterNum.set(MAX_NUM);
        adapter.removeAllChapter();
        showAddSignLayout.set(false);
        isShow.set(true);
    }

    /**
     * 签署合同-添加印章(最多3个) 点击事件
     */
    public void contractSignAddSealOnClick() {
        if (maxChapterNum.get() > 0) {
            adapter.addChapter(Integer.parseInt(currentPosition.get().substring(0, currentPosition.get().indexOf("/"))) - 1);
            maxChapterNum.set(maxChapterNum.get() - 1);
        }
    }

    /**
     * 获取验证码
     */
    public void getValidationCode() {
        showLoading();
        ContractNet.getInstance().getContractApi().getSignContractCode(bean.no).enqueue(new AbstractCallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // 获取验证码成功
                countDownTimer.start();
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                super.onFailure(call, t);
                countDownTimer.cancel();
            }
        });
    }

    /**
     * 判断是否已经过签署截止日期
     */
    private boolean validationSignDeadline() {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            Date deadlineDate = sdf.parse(bean.deadline);
            if (new Date().compareTo(deadlineDate) >= 0) {
                showDialog("timeFailure", "已超过签署截止日期\n合同已失效！");
                return false;
            }
        } catch (Throwable e) {
            MLog.e("timeFailure", e.getMessage());
            return false;
        }
        return true;
    }

    CountDownTimer countDownTimer = new CountDownTimer(60 * 1000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            int currentTime = Integer.parseInt(Objects.requireNonNull(showValCodeCountdownLayout.get()));
            showValCodeCountdownLayout.set(String.valueOf(--currentTime));
        }

        @Override
        public void onFinish() {
            showValCodeCountdownLayout.set("60");
        }
    };

    /**
     * 关闭获取验证码布局
     */
    public void closeGetValidationCodeLayout() {
        adapter.setDragEnabled(true);
        showValidationCodeLayout.set(false);
        countDownTimer.cancel();
        countDownTimer.onFinish();
    }

    /**
     * 签署合同-立即签署 校验校验校验 点击事件
     */
    public void signContractCheckOnClick() {
        if (adapter.hasChapterOverlap()) {
            showDialog("hasChapterOverlap", "当前合同第" + currentPosition.get() + "页存在印章覆盖\n请重新拖拽！");
            return;
        }
        if (!adapter.hasAddChapter()) {
            showDialog("hasAddChapter", "请签署印章后再提交签署");
            return;
        }
        // 已超过签署截止日期判断
        if (!validationSignDeadline()) {
            return;
        }
        // 校验合同状态
        showLoading();
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        showValidationCodeLayout.set(true);
                        adapter.setDragEnabled(false);
                        // getSignLocationList();
                        // 获取安心签验证码
                        getValidationCode();
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {
                        showDialog("checkStatusByNowStatus", entity.getMsg());
                    }
                });
    }

    /**
     * 获取合同详情
     */
    public void getContractDetail(String contractId) {
        getContractDetail(contractId, false);
    }

    /**
     * 获取合同详情
     */
    public void getContractDetail(String contractId, boolean isLookContract) {
        showLoading();
        // 获取企业信息
        getCompanyDetail();
        // 获取合同详情
        ContractNet.getInstance().getContractApi().getContractDetail(contractId).enqueue(new AbstractCallBack<BaseEntity<ContractDetailBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractDetailBean> data) {
                bean = data.getData();
                isSendApproval = false;
                if (!isLookContract) {
                    // 设置操作名称
                    contractOperationName.set("退回");
                    if ("1".equals(bean.status) && !showOperationLayout.get()) {
                        isSendContract = true;
                        showOperationLayout.set(true);
                        isShow.set(false);
                    }
                    contractOperationName1.set(isSendContract ? "发送合同" : "签署合同");
                    contractOperationName2.set("修改合同");
                    if ("2".equals(bean.status) && "7".equals(String.valueOf(bean.appSignTag))) {
                        //开启审批（未开启审批走原来的流程）
                        if (bean.openAudit) {
                            if (bean.appAuditStatus == 0) {
                                isSendApproval = true;
                            }
                        }
                        //审核中
                        if (bean.appAuditStatus == 1) {
                            showInApprovalLayout.set(true);
                            isShow.set(false);
                        } else if (bean.appAuditStatus == 3) {
                            isSendApproval = true;
                        }
                        contractOperationName1.set(isSendApproval ? "提交审批" : "签署合同");
                        showOperationLayout.set(true);
                    }
                }

                if (null != bean && null != bean.localUrl) {
                    // 加载合同详情合同图片
                    adapter.setDatas(Arrays.asList(bean.localUrl.split(",")));
                    pageNum = adapter.getItemCount();
                    currentPosition.set("1/" + pageNum);
                    adapter.notifyDataSetChanged();
                    // 注册动态的holder 加载监听器
                    adapter.setHolderCreateListener(SignContractViewModel.this);
                } else {
                    // 显示无数据
                    showOperationLayout.set(false);
                    showNoDataView.postValue(null);
                }
                // 判断是否有盖章
                if (bean.involves == null || bean.involves.size() == 0) {
                    isShow.set(true);
                    return;
                }
                for (ContractDetailBean.InvolvesInfo info : bean.involves) {
                    if (info.signStatus == 1) {
                        isShow.set(false);
                    }
                }
            }
        });
    }

    /**
     * 获取企业信息
     */
    public void getCompanyDetail() {
        showLoading();
        ContractNet.getInstance().getContractApi().getCompanyDetail().enqueue(new AbstractCallBack<BaseEntity<OpenAccountDetailEntity>>() {
            @Override
            public void onSuccess(BaseEntity<OpenAccountDetailEntity> data) {
                phoneNum.set(StringUtils.handlePhoneNum(data.getData().phone));
                name.set(data.getData().name);
            }
        });
    }

    /**
     * 签署合同
     */
    public void signContractOnClick() {
        countDownTimer.cancel();
        if (StringUtils.isEmpty(validationCode.get())) {
            MToast.showToast(application.getApplicationContext(), "请输入验证码");
            return;
        }
        ContractNet.getInstance().getContractApi().checkStatusByNowStatus(bean.no, String.valueOf(bean.status), bean.appSignStatus, bean.appSignTag, bean.appAuditStatus)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("code", validationCode.get());
                        map.put("contractNo", bean.no);
                        map.put("signLocations", getSignLocationList());
                        showLoading();

                        // 找对方的签署状态
                        for (ContractDetailBean.InvolvesInfo info : bean.involves) {
                            if (bean.isTaker && info.isTaker == 1) {
                            } else if (bean.isTaker == false && info.isTaker == 0) {
                            } else {
                                mSignStatus = info.signStatus;
                            }
                        }
                        ContractNet.getInstance().getContractApi().signContract(map).enqueue(new AbstractCallBack<BaseEntity<Object>>() {
                            @Override
                            public void onSuccess(BaseEntity<Object> data) {
                                successDesc.set(mSignStatus == 1 ? "恭喜您，已成功签署合同\n如需下载合同，请前往电脑端" : "恭喜您，已成功签署合同请等待对方签署");
                                showSignSuccessLayout.set(true);
                            }
                        });
                    }

                    @Override
                    public void onBusinessFailure(BaseEntity entity) {

                        if (Constants.NET_BSS_ERROR_CODE_539114.equals(entity.getErrCode())) {
                            showDialog("status", entity.getMsg());
                        } else {
                            showDialog("checkStatusByNowStatus", entity.getMsg());
                        }
                    }
                });
    }

    /**
     * 查看合同详情 - 点击事件
     */
    public void lookContractDetails() {
        // 关闭获取验证码弹窗布局
        showValidationCodeLayout.set(false);
        // 不显示操作布局
        showOperationLayout.set(false);
        // 清除adapter
        adapter.removeAllChapter();
        adapter.setDatas(null);
        adapter.notifyDataSetChanged();
        // 查看合同，刷新合同
        getContractDetail(String.valueOf(bean.id), true);
    }

    /**
     * 获取坐标
     */
    private List<ContractDetailBean.SignLocations> getSignLocationList() {
        List<ContractDetailBean.SignLocations> signLocationList = new ArrayList<>();
        for (Map.Entry<String, CustomDragView.DragParams> entry : adapter.getDragParamsMap().entrySet()) {
            // 后端要的业务坐标点数据为正方形印章的左下角坐标和右上角坐标，因此此处做业务坐标点转换
            ContractDetailBean.SignLocations signLocations = new ContractDetailBean.SignLocations();
            String position = entry.getKey().substring(0, entry.getKey().indexOf("|"));
            signLocations.signOnPage = String.valueOf(Integer.parseInt(position) + 1);
            signLocations.signLocationLBX = String.valueOf((int) entry.getValue().x);
            signLocations.signLocationLBY = String.valueOf(Math.max((int) (entry.getValue().y - adapter.getChapterWidth()), 0));
            signLocations.signLocationRUX = String.valueOf(Math.max((int) (entry.getValue().x + adapter.getChapterWidth()), 0));
            signLocations.signLocationRUY = String.valueOf(Math.max((int) entry.getValue().y, adapter.getChapterWidth()));
            MLog.d("添加的印章坐标系为: ", signLocations.signLocationLBX + "| " + signLocations.signLocationLBY + "| " +
                    signLocations.signLocationRUX + "| " + signLocations.signLocationRUY);
            signLocationList.add(signLocations);
        }
        return signLocationList;
    }

    /**
     * 根据滚动点大小计算当前RecyclerView滑动到的position点
     * 注意:该函数只适应于本示例。以下获取父容器滚动区间命中计算为后续扩展使用
     * <p>
     * 父容器滚动距离计算方法
     * int visibleItemPosition = layoutManager.findFirstVisibleItemPosition();
     * View currentVisibleItemView = layoutManager.findViewByPosition(visibleItemPosition);
     * int currentViewHeight = null == currentVisibleItemView ? 0 : currentVisibleItemView.getHeight();
     * int scrollY = (visibleItemPosition) * currentViewHeight - currentVisibleItemView.getTop();
     * </p>
     *
     * @param recyclerView           recyclerView
     * @param positionProportionSize position的滑动判断比例大小，超过此距离则判定滑动到下一个item了
     */
    public void calcCurrentPosition(RecyclerView recyclerView, int positionProportionSize) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        if (null == layoutManager || layoutManager.getChildCount() <= 0) {
            return;
        }
        // 以左上角为初始坐标当前 viewItem 项position，position从0开始
        int visibleItemPosition = layoutManager.findFirstVisibleItemPosition();
        View nextView = layoutManager.findViewByPosition(visibleItemPosition + 1);

        // 已经是最后一个item的position位置，不做逻辑操作
        if (null == nextView) {
            return;
        }
        // 永远对下一个item的position做有效的滑动判断
        int calcNextHeight = nextView.getHeight() - nextView.getTop();
        // 当下一个item的滚动距离大于等于滚动比例点，则记录到下一个位置
        if (calcNextHeight >= positionProportionSize) {
            currentPosition.set((visibleItemPosition + 2) + "/" + pageNum);
        } else {
            currentPosition.set((visibleItemPosition + 1) + "/" + pageNum);
        }
    }

    @Override
    public void notifyAny(Object obj) {
        if (null != bean.signLocations && obj instanceof Integer) {
            // 2021-09-01   暂时不用回显
            // 根据recyclerView 的动态加载item 视图来动态添加印章
//            for(ContractDetailBean.SignLocations signLocations : bean.signLocations) {
//                if(String.valueOf(obj).equals(signLocations.signOnPage)) {
//                    adapter.addChapter(Integer.parseInt(signLocations.signOnPage) - 1,
//                            Integer.parseInt(signLocations.signLocationLBX), Integer.parseInt(signLocations.signLocationRUY));
//                }
//            }
        }
    }

    /**
     * 合同是否开启审批
     *
     * @param id         合同ID
     * @param auditType  审批类型：发送审批 sendContract 签署审批 signContract
     * @param actionType 2.提交审批  3.签署合同
     */
    private void checkApprovalOpenAudit(long id, String auditType, int actionType) {
        ContractNet.getInstance().getContractApi().checkOpenAudit(auditType)
                .enqueue(new AbstractCallBack<BaseEntity<Boolean>>() {
                    @Override
                    public void onSuccess(BaseEntity<Boolean> data) {
                        if (data.getData()) {
                            findAuditStatus(id, auditType, actionType);
                        } else {
                            if (3 == actionType) {
                                // 签署合同
                                showAddSignLayout.set(true);
                                isShow.set(false);
                            } else {
                                showDialog("checkApprovalOpenAudit", "当前合同审批状态已变更！");
                            }
                        }
                    }
                });
    }

    /**
     * 查询合同的审批状态
     *
     * @param id         合同id
     * @param auditType  auditType 审批类型：发送审批 sendContract 签署审批 signContract
     * @param actionType 1 退回 2提交审批  3 签署合同 4 修改合同
     */
    void findAuditStatus(long id, String auditType, int actionType) {
        ContractNet.getInstance().getContractApi().findAuditStatus(id, auditType).enqueue(new AbstractCallBack<BaseEntity<ContractApprovalStatusBean>>() {
            @Override
            public void onSuccess(BaseEntity<ContractApprovalStatusBean> data) {
                if (3 == actionType) {
                    if (2 == data.getData().auditStatus) {
                        // 签署合同
                        showAddSignLayout.set(true);
                        isShow.set(false);
                    } else if (3 == data.getData().auditStatus) {
                        ARouter.getInstance().build(ArouterConfigs.AR_CONTRACT_APPROVAL)
                                .withSerializable("CONTRACT_DETAIL", bean)
                                .navigation();
                    }
                } else {
                    if (data.getData().auditStatus == bean.appAuditStatus) {
                        //开通审批并且审批状态没有发生变化
                        if (1 == actionType) {
                            showDialog("discardContract", "退回合同后不可恢复" +
                                    "\n还要继续吗？", "取消", "退回");
                        } else if (4 == actionType) {
                            showDialog("", "请前往电脑端进行合同编辑！");
                        } else if (2 == actionType) {
                            ARouter.getInstance().build(ArouterConfigs.AR_CONTRACT_APPROVAL)
                                    .withSerializable("CONTRACT_DETAIL", bean)
                                    .navigation();
                        }
                    } else {
                        showDialog("findAuditStatus", "当前合同审批状态已变更！");
                    }
                }
            }
        });
    }
}
